package com.xmy.study.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.study.entity.PerStudentClassType;
import com.xmy.study.entity.WillingnessSurvey;
import com.xmy.study.entity.response.WillingnessSurveyOption;
import com.xmy.study.feign.IStudentErpFeignClient;
import com.xmy.study.service.IPerStudentClassTypeService;
import com.xmy.study.service.IWillingnessSurveyService;
import com.xmy.study.util.Code;
import com.xmy.study.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/web/willingnessSurvey")
public class WillingnessSurveyController extends WebBaseController {

    @Autowired
    IWillingnessSurveyService iWillingnessSurveyService;

    @Autowired
    IPerStudentClassTypeService iPerStudentClassTypeService;

    @Autowired
    IStudentErpFeignClient iStudentErpFeignClient;

    @GetMapping()
    @ApiOperation("验证是否提交过")
    public Result ifTo(@RequestParam Map<String,Object> reMap) {
        int subjectsId = 2;
        if (reMap.get("subjectsId") != null && !reMap.get("subjectsId").toString().isEmpty()) {
            subjectsId = Integer.parseInt(reMap.get("subjectsId").toString());
        }
        long count = iWillingnessSurveyService.getCount(new QueryWrapper<WillingnessSurvey>()
                .eq("student_id", this.studentId)
                .eq("year_part",this.yearPart)
                .eq("quarter_num",this.quarterNum)
                .eq("subjects_id", subjectsId)
                .eq("is_fill",1));
        String msg = "未提交过";
        if (count > 0) {
            msg = "已提交过";
            count = 1;
        }
        return new Result(Code.OK,count,msg);
    }

    @PostMapping()
    public Result save(@RequestBody Map<String,Object> reMap) {
        if (reMap.get("answer") != null && !reMap.get("answer").toString().isEmpty()) {
            int subjectsId = 2;
            if (reMap.get("subjectsId") != null && !reMap.get("subjectsId").toString().isEmpty()) {
                subjectsId = Integer.parseInt(reMap.get("subjectsId").toString());
            }
            List<WillingnessSurvey> answerRs = JSON.parseArray(JSON.toJSONString(reMap.get("answer")), WillingnessSurvey.class);
            long timestamp = System.currentTimeMillis();
            PerStudentClassType perStudentClassType = new PerStudentClassType();
            perStudentClassType.setStudentId(this.studentId);
            perStudentClassType.setYearPart(this.yearPart);
            perStudentClassType.setQuarterNum(this.quarterNum);
            perStudentClassType.setSubjectsId((long) subjectsId);
            PerStudentClassType perStudentClassTypeServiceOne = iPerStudentClassTypeService.getPerStudentClassType(perStudentClassType);
            if (perStudentClassTypeServiceOne == null) {
                return new Result(Code.ERROR,"","学员不存在！");
            }
            for (WillingnessSurvey willingnessSurvey : answerRs) {
                willingnessSurvey.setAddTime(timestamp);
                willingnessSurvey.setSubjectsId(subjectsId);
                if (perStudentClassTypeServiceOne != null) {
                    willingnessSurvey.setSchoolId(perStudentClassTypeServiceOne.getSchoolId());
                    willingnessSurvey.setGradeId(perStudentClassTypeServiceOne.getGradeId());
                    willingnessSurvey.setTeacherId(perStudentClassTypeServiceOne.getTeacherId());
                    willingnessSurvey.setTeacherName(perStudentClassTypeServiceOne.getTeacherName());
                    willingnessSurvey.setClassTypeId(perStudentClassTypeServiceOne.getClassTypeId());
                    willingnessSurvey.setYearClassId(perStudentClassTypeServiceOne.getYearClassId());
                    willingnessSurvey.setStudentName(this.studentAccountInfo.getStudentName());
                }
                saveHandle(willingnessSurvey);
            }
        }
        return new Result(Code.OK,"","提交成功！");
    }

    private void saveHandle(WillingnessSurvey willingnessSurvey) {
        long count = iWillingnessSurveyService.getCount(new QueryWrapper<WillingnessSurvey>()
                .eq("student_id", this.studentId)
                .eq("type",willingnessSurvey.getType())
                .eq("subjects_id",willingnessSurvey.getSubjectsId())
//                .eq("add_time", willingnessSurvey.getAddTime())
                .eq("year_part",this.yearPart)
                .eq("quarter_num",this.quarterNum)
                .eq("is_fill",1)
                .eq("subjects_id", willingnessSurvey.getSubjectsId()));
        if (count > 0) {
            return ;
        }
        willingnessSurvey.setStudentId(this.studentId);
        willingnessSurvey.setYearPart(this.yearPart);
        willingnessSurvey.setQuarterNum(this.quarterNum);
        iWillingnessSurveyService.goToSave(willingnessSurvey);
    }

    @GetMapping("option")
    @ApiOperation("选项列表")
    public Result option() {
        List<WillingnessSurveyOption> willingnessSurveyOptionList = iStudentErpFeignClient.getWillingnessSurveyOptionList();
        return new Result(Code.OK,willingnessSurveyOptionList,"选项列表成功！");
    }
}
